package thread_pool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/18 4:44 PM
 */
public class TestRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
        System.out.println(Thread.currentThread().getName() + "----------------------------> 进入拒绝策略");

        // 延迟执行
        try {
            Thread.sleep(5L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 放入队列
        BlockingQueue<Runnable> workQueue = executor.getQueue();
        if (workQueue.offer(runnable)) {
            return;
        }

        // 新建线程执行
        new Thread(runnable, "【拒绝策略线程】").start();

        // OR
        // 放弃此任务，记录日志


    }
}
